<?php
// +----------------------------------------------------------------------
// | 用户已购套餐
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;

class UserYgMeal extends Model
{
	
	//商品，一对多关联
	public function ygProj(){
        return $this->hasMany('UserYgProj','meal_id','id');
    }
		
	//商品，一对一关联
	public function goods(){
        return $this->hasOne('goods','id','goods_id')->bind([
        	'goods_name'=>'name',
        	'goods_img'=>'img',
        ]);
    }
    
 	//套餐内商品列表，一对多关联 
	public function goodsMeal(){
        return $this->hasMany('GoodsMeal','pid','goods_id');
    }
    
    //商品规格，一对一关联
	public function specs(){
        return $this->hasOne('goodsSpecs','id','specs_id')->bind([
        	'specs_name'=>'name',
        	'specs_img'=>'img',
        ]);
    }
		
		
    //获取已购套餐
	public function getYgMeal($uid,$shopId=-1){
		
      	$model = self::with(['goods'])->alias('a')
      		->leftJoin('user b','a.uid = b.id')
      		->leftJoin('shop c','c.id = a.shop_id')
      		->field('a.*')
			->field('c.name as shop_name')
      		->where('a.uid',$uid)
      		//->where('b.shop_id',$shopId)
      		->order('a.id','desc');
      	
		if($shopId !=-1){
			$model = $model->where('a.shop_id',$shopId);
		}
		
		$result = $model->select();
			
			
		foreach($result as $key => $meal ){
			$result[$key]['total']  = $meal->ygProj()->sum('num');
			$result[$key]['surplus']  = $result[$key]['total'] - $meal->ygProj()->sum('use_num');
			//状态
			$d1 = explode('-',$meal->add_time);
			$d2 = explode('-',date("Y-m-d"));
			$res = abs($d1[0] - $d2[0]) * 12 + abs($d1[1] - $d2[1]);
			if( $res > $result[$key]['effective_time']){
				$result[$key]['status']  = '已过期';
			}else{
				$result[$key]['status']  = $result[$key]['surplus'] > 0 ? '使用中' : '已用完';
			}
		}
		return $result;
    }
    
    
    //套餐明细
	public function details($id){
      	$meal = self::with(['goods','ygProj'=>function($query){
  			$query->with('goods');
  			$query->group('specs_id');
  		}])->get($id);
      		
      	if(!$meal){
      		throw new \think\Exception('没有该服务!');
      	}
      	
      	$meal['total']  = $totals = $meal->ygProj()->sum('num');
		$meal['surplus']  = $totals - $meal->ygProj()->sum('use_num');
		
		foreach($meal->yg_proj as $key => $value ){
			$specsId = $value->specs_id;
      		$meal['yg_proj'][$key]['total'] = $total =  $meal->ygProj()->where('specs_id',$specsId)->sum('num');
      		$meal['yg_proj'][$key]['surplus'] = $total - $meal->ygProj()->where('specs_id',$specsId)->sum('use_num');
      	}
      	
      	//状态
		$d1 = explode('-',$meal->add_time);
		$d2 = explode('-',date("Y-m-d"));
		$res = abs($d1[0] - $d2[0]) * 12 + abs($d1[1] - $d2[1]);
		if( $res > $meal['effective_time'] ){
			$meal['status']  = '已过期';
		}else{
			$meal['status']  = $meal['surplus'] > 0 ? '使用中' : '已用完';
		}
      	//过期时间
      	$meal['expire_time']  = date("Y-m-d H:m:s", strtotime("+$meal[effective_time] months", strtotime($meal['add_time'])));
      	
		return $meal;
    }
    
}
